﻿
using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace BLL
{




    public class Kind
    {

        public static string GetXML_TagName(string kind,string TagName)           //获得xml中的SQL地址
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Windows.Forms.Application.StartupPath + @"\XML\Ydhl_XML\print.xml");
            XmlElement xmlRoot = xmlDoc.DocumentElement;
            string select = "//Kind/" + kind;
            XmlNodeList topM = xmlDoc.SelectNodes(select);
            foreach (XmlElement element in topM)
            {
                string SQL = element.GetElementsByTagName(TagName)[0].InnerText;
                return SQL;
            }
            return null;
        }

        public static string getFileName()   //获取当前class的名称
        {
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1);//1代表上级，2代表上上级，以此类推
            MethodBase method = frame.GetMethod();
            return  method.ReflectedType.Name;
           
        }



        public static string GetLocalIP()     //获得ip
        {
            //获取说有网卡信息
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    //获取以太网卡网络接口信息
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    //获取单播地址集
                    UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {

                        if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        //判断是否为ipv4
                        {
                            string strLocalIP = ipadd.Address.ToString();//获取ip                                                       
                            return strLocalIP;
                        }
                    }

                }
            }
            return null;
        }


        #region 通过NetworkInterface获取MAC地址
        /// <summary>
        /// 通过NetworkInterface获取MAC地址
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            try
            {
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in interfaces)
                {
                    return BitConverter.ToString(ni.GetPhysicalAddress().GetAddressBytes());
                }
            }
            catch (Exception)
            {
            }
            return "00-00-00-00-00-00";
        }
        #endregion

        /// <summary>
        /// 获得字段拼音全，如吃饭返回CHIFAN
        /// </summary>
        /// <returns></returns>
        public static string Convert(string chr)
        {
            try
            {
                if (chr.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < chr.Length; i++)
                    {
                        bool isChineses = ChineseChar.IsValidChar(chr[i]);
                        
                        if (isChineses)
                        {
                            ChineseChar chineseChar = new ChineseChar(chr[i]);
                            
                            foreach (string value in chineseChar.Pinyins)
                            {
                                if (!string.IsNullOrEmpty(value))
                                {
                                    fullSpell.Append(value.Remove(value.Length - 1, 1));
                                    break;
                                }
                            }
                        }
                        else
                        {
                            fullSpell.Append(chr[i]);
                        }
                    }

                    return fullSpell.ToString().ToUpper();
                }
            }
            catch (Exception)
            {
               
            }

            return string.Empty;
        }

        /// <summary>
        /// 获取首字母，吃饭返回C
        /// </summary>
        public static string SubFirstLetter(string chr)
        {
            try
            {
                chr = chr.Substring(0, 1);
                if (chr.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < chr.Length; i++)
                    {
                        bool isChineses = ChineseChar.IsValidChar(chr[i]);
                        if (isChineses)
                        {
                            ChineseChar chineseChar = new ChineseChar(chr[i]);
                            foreach (string value in chineseChar.Pinyins)
                            {
                                if (!string.IsNullOrEmpty(value))
                                {
                                    fullSpell.Append(value.Remove(value.Length - 1, 1));
                                    break;
                                }
                            }
                        }
                        else
                        {
                            fullSpell.Append(chr[i]);
                        }
                    }

                    return fullSpell.ToString().Substring(0, 1).ToUpper();
                }
            }
            catch (Exception)
            {
                
            }

            return string.Empty;
        }
    }

}


